# -*- coding: utf-8 -*-
"""
标题场景模板 - 专门用于标题展示
"""

from typing import Any, Dict, Optional
from pathlib import Path

from .base_template import BaseSceneTemplate


class TitleSceneTemplate(BaseSceneTemplate):
    """标题场景模板"""

    def __init__(
        self,
        title: str = "",
        subtitle: str = "",
        audio_duration: float = 0.0,
        subtitle_file: Optional[Path] = None,
        theme=None,
        title_size: str = "title",
    ):
        """
        初始化标题场景

        Args:
            title: 主标题
            subtitle: 副标题
            audio_duration: 音频时长
            subtitle_file: 字幕文件
            theme: 主题
            title_size: 标题大小类型
        """
        super().__init__(title, subtitle, audio_duration, subtitle_file, theme)
        self.subtitle = subtitle
        self.title_size = title_size

    def create_content(self, scene) -> Dict[str, Any]:
        """
        创建标题内容

        Args:
            scene: Manim场景对象

        Returns:
            包含标题和副标题的字典
        """
        content = {}

        # 创建主标题
        if self.title:
            title_style = self.get_style(self.title_size)
            title_obj = scene.Text(
                self.title,
                font_size=title_style["font_size"],
                color=title_style["color"],
                font=title_style["font"],
            )
            # 居中显示
            title_obj.center()
            content["title"] = title_obj

        # 创建副标题
        if self.subtitle:
            subtitle_style = self.get_style("subtitle")
            subtitle_obj = scene.Text(
                self.subtitle,
                font_size=subtitle_style["font_size"],
                color=subtitle_style["color"],
                font=subtitle_style["font"],
            )
            # 放在主标题下方
            if "title" in content:
                subtitle_obj.next_to(content["title"], scene.DOWN, buff=0.3)
            else:
                subtitle_obj.center()
            content["subtitle"] = subtitle_obj

        return content

    def animate_content(self, scene, content: Dict[str, Any]) -> None:
        """
        动画标题内容

        Args:
            scene: Manim场景对象
            content: 内容字典

        Returns:
            None
        """
        animations = []

        # 标题动画
        if "title" in content:
            title = content["title"]
            title.scale(0)  # 从0开始缩放
            animations.append(
                scene.scale(title, 1.0, run_time=self.get_animation_config("default_duration"))
            )

        # 副标题动画
        if "subtitle" in content:
            subtitle = content["subtitle"]
            subtitle.scale(0)  # 从0开始缩放
            animations.append(
                scene.scale(subtitle, 1.0, run_time=self.get_animation_config("default_duration"))
            )

        # 播放所有动画
        for anim in animations:
            scene.play(anim)

            # 如果使用了TimeScaler，等待相应时间
            if self.scaler:
                self.scaler.current_time += anim.run_time
